package com.algomypractice.linkedlist;

import com.sourcestruct.linearlist.SingleLinkedList;

/**
 * @author: ZhouBert
 * @date: 2019/6/7
 * @description: 找到单链表的中间节点
 */
public class SingleLinkedMiddle {

	public static void main(String[] args) {
		SingleLinkedList lastnode = new SingleLinkedList(9, null);
		SingleLinkedList nodea = new SingleLinkedList(7, lastnode);
		SingleLinkedList nodeb = new SingleLinkedList(5, nodea);
		SingleLinkedList nodec = new SingleLinkedList(3, nodeb);
		SingleLinkedList noded = new SingleLinkedList(1, nodec);
		System.out.println(noded);

		System.out.println(getMiddleByTwoPoint(noded));
	}

	/**
	 * 有了上个题目《删除链表倒数第N个节点》的经验，首先想到通过两个快慢指针去进行一次遍历，只要让快慢指针相差一遍速度就可以了。
	 *
	 * @param node
	 * @return
	 */
	public static SingleLinkedList getMiddleByTwoPoint(SingleLinkedList node) {
		//屏蔽边界条件判断

		int type = 0;
		SingleLinkedList slow = node;
		SingleLinkedList fast = node;
		while (fast.getNext() != null && fast.getNext().getNext() != null) {
			fast = fast.getFast();
			slow = slow.getSlow();
		}
		return slow;
	}


}
